package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.AddressException;
import com.huahang.fashionpress.form.FormAddress;
import com.huahang.fashionpress.service.impl.AddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressServiceImpl addressService;

    @RequestMapping("/")
    public String getAddressView(){
        return "client/addAddress";
    }

    @RequestMapping("/add")
    public ModelAndView addAddress(String addressBase, String addressPlace, String addressName,
                                   String addressPhone, Integer addressStatus, HttpSession session){
        //获取用户id
        Integer userID = (Integer) session.getAttribute("userID");
        //封装对象
        FormAddress formAddress = new FormAddress();
        formAddress.setUserID(userID);
        formAddress.setAddressBase(addressBase);
        formAddress.setAddressPlace(addressPlace);
        formAddress.setAddressName(addressName);
        formAddress.setAddressPhone(addressPhone);
        formAddress.setAddressStatus(addressStatus);
        //添加地址
        boolean result = addressService.addNewAddress(formAddress);

        if(!result){
            throw new AddressException(ExceptionEnum.ADDRESS_ERROR);
        }

        return new ModelAndView("redirect:/index");
    }
}
